package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void add(@RequestBody Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);

    }

    @Override
    public PageResult<Dept> query(String name, Integer status, Integer page, Integer pageSize) {
        /**
         * 分页
         */
        PageHelper.startPage(page, pageSize);

        /**
         * 查询
         */
        List<Dept> list = deptMapper.selectByCondition(name, status);
        /**
         * 封装分页结果
         */

       Page<Dept> result = (Page<Dept>) list;

   return new PageResult<>(result.getTotal(), result.getResult());
    }

    @Override
    public Dept selectById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public List<Dept> findAll() {
        List<Dept> list = deptMapper.selectAll();
        return list;
    }
}


